package com.ebocy.hammer.core.domain;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.ebocy.hammer.core.GameCache;

//TODO ---------需要将此类以及相关的业务逻辑最后放在battleServer
/**
 * 房间 2012-4-9@author yangshaobo
 */
public class Room {

	public int roomId;

	public String mapName = "";

	/** 队伍人数 */
	public int teamNum;

	/** 房间状态,0为组队,1为battle服务器,2为开打,3为占位 */
	public int state = 0;

	/**
	 * 战斗服务器中失去连接列表
	 */
	public Set<Long> losts = new HashSet<Long>();

	/**
	 * 资源加载完成人员
	 */
	public Set<Long> inits = new HashSet<Long>();

	/**
	 * 战斗房间人员
	 */
	public Set<Long> battles = new HashSet<Long>();

	/**
	 * 组队房间人员
	 */
	public Set<Long> worlds;

	/**
	 * 房间道具缓存 key 为道具唯一id.. value 为地块信息 x y 坐标
	 */
	public Map<Integer, Integer[]> mapBlock = new ConcurrentHashMap<Integer, Integer[]>();

	/**
	 * 获取场景物品id
	 */
	public int getUUID() {
		return GameCache.getRoomIncr(roomId);
	}

	/**
	 * 保存房间信息到redis
	 */
	public void saveRoome() {
		GameCache.setRoom(this);
	}

	/**
	 * 保存房间信息到redis
	 */
	public void saveRoome(int state) {
		// 状态发生改变后,更新reids缓存
		if (this.state != state) {
			this.state = state;
			GameCache.setRoom(this);
		}
	}

	public void addWorld(Long gamePlayerId, GamePlayer gamePlayer) {
		worlds.add(gamePlayerId);
		teamNum++;
		GameCache.setRoom(this);
		GameCache.getInstance().addCache(gamePlayer);
	}

	public void delWorld(Long gamePlayerId) {
		worlds.remove(gamePlayerId);
		if (worlds.size() == 0 && state == 0) {
			GameCache.delQueue();
			GameCache.delRoom(roomId);
		} else {
			teamNum--;
			GameCache.setRoom(this);
			GameCache.getInstance().delCache(gamePlayerId);
		}
	}

	public void addLost(Long gamePlayerId) {
		losts.add(gamePlayerId);
		GameCache.setRoom(this);
	}

	public void addInit(Long gamePlayerId) {
		inits.add(gamePlayerId);
		GameCache.setRoom(this);
	}

	public void addBattle(Long gamePlayerId, GamePlayer gamePlayer) {
		battles.add(gamePlayerId);
		GameCache.setRoom(this);
		GameCache.getInstance().addCache(gamePlayer);
	}

	public void delBattle(Long gamePlayerId) {
		battles.remove(gamePlayerId);
		if (battles.size() == 0) {
			GameCache.delRoom(roomId);
		} else {
			GameCache.setRoom(this);
			GameCache.getInstance().delCache(gamePlayerId);
		}
	}

	public void setResInit(Long gamePlayerId) {
		inits.add(gamePlayerId);
		GameCache.setRoom(this);
	}

	// public Room(long id) {
	// this.id = id;

	// }
	// private long id;
	// private Set<GamePlayer> gamePlayers=new HashSet<GamePlayer>();
	// public long getId() {
	// return id;
	// }
	// public void setId(long id) {
	// this.id = id;
	// }
	// public Set<GamePlayer> getGamePlayers() {
	// return gamePlayers;
	// }
	// public void setGamePlayers(Set<GamePlayer> gamePlayers) {
	// this.gamePlayers = gamePlayers;
	// }
	// public void addGamePlayer(GamePlayer gamePlayer){
	// gamePlayer.setRoomId(id);
	// this.gamePlayers.add(gamePlayer);
	// }
	// public void removeGamePlayer(GamePlayer gamePlayer){
	// this.gamePlayers.remove(gamePlayer);
	// }
	// public int size(){
	// return this.gamePlayers.size();
	// }

}
